package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


import com.bifrii.transferObjects.Role;

public class JDBCRoleDAO implements IRoleDAO {

	@Override
	public List<Role> selectAllRoles() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Role> searchResults = new LinkedList<Role>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Role order by idRole desc";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Role temp = new Role();
                temp.setOid(result.getString("oid"));
                temp.setIdRole(result.getInt("idRole"));
                temp.setName(result.getString("name"));
                temp.setPrivilege(result.getString("privilege"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;

	}

}
